using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Globalization;
using Lilium.Web.Mvc.AjaxControlToolkit;

namespace Samples.Controllers
{
	public class PopupControlController: Controller
	{
		//
		// GET: /PopupControl/

		public ActionResult Index(
			string dateTextBox,
			string reminderDate,
			string messageTextBox,
			string reminderMessage,
			string reminderText,
			string clientAction)
		{
			ViewData["DateTextBox"] = dateTextBox;
			ViewData["ReminderDate"] = reminderDate;
			ViewData["MessageTextBox"] = messageTextBox;
			ViewData["ReminderMessage"] = reminderMessage;
			ViewData["ReminderText"] = reminderText;
			ViewData["ClientAction"] = clientAction;

			switch (clientAction)
			{
				case "SelectDate":
					ViewData["DateTextBox"] = reminderDate;
					break;
				case "SelectMessage":
					switch (reminderMessage)
					{
						case "WalkDog":
							ViewData["MessageTextBox"] = "Walk dog";
							ViewData["ReminderMessage"] = "Walk dog";
							break;
						case "FeedDog":
							ViewData["MessageTextBox"] = "Feed dog";
							ViewData["ReminderMessage"] = "Feed dog";
							break;
						case "FeedCat":
							ViewData["MessageTextBox"] = "Feed cat";
							ViewData["ReminderMessage"] = "Feed cat";
							break;
						case "FeedFish":
							ViewData["MessageTextBox"] = "Feed fish";
							ViewData["ReminderMessage"] = "Feed fish";
							break;
						default:
							ViewData["ReminderMessage"] = string.Empty;
							break;
					}
					break;
				case "Submit":
					try
					{
						var selectedDate = DateTime.Parse(dateTextBox, CultureInfo.InvariantCulture).ToLongDateString();
						ViewData["ReminderText"] = string.Format(
							"A reminder would have been created for {0} with the message \"{1}\"",
							selectedDate,
							messageTextBox);
					}
					catch
					{
						ViewData["ReminderText"] = string.Format("[Unable to parse \"{0}\": {1}]", dateTextBox, messageTextBox);
					}
					break;
				default:
					ViewData["ReminderText"] = "[No response provided yet]";
					break;
			}

			if (Request.IsAjaxRequest())
			{
				return PartialView("ReminderControl");
			}
			else
				return View();
		}
	}
}
